package com.cema.manage.project.manage.questionBank.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cema.manage.project.manage.questionBank.domain.QuestionBank;
import com.cema.manage.project.manage.questionBank.mapper.QuestionBankMapper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 成语题库 服务层实现
 *
 * @author reasahi
 * @date 2024-07-12
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements IQuestionBankService {

    public void redXls(InputStream fileInputStream) throws IOException {
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
        int index = 0;
        List<QuestionBank> questionBankList = new ArrayList<>();
        for (Row row : sheet) { // 遍历行
            if (index == 0) {
                index++;
                continue;
            }
            QuestionBank questionBank = new QuestionBank();
            String value = row.getCell(0).getStringCellValue();
            if (StrUtil.isEmpty(value)) {
                continue;
            }
            questionBank.setQbLevel(row.getCell(0).getStringCellValue());
            questionBank.setQbContent(row.getCell(1).getStringCellValue());
            questionBank.setQbValueA(row.getCell(2).getStringCellValue());
            questionBank.setQbValueB(row.getCell(3).getStringCellValue());
            questionBank.setQbValueC(row.getCell(4).getStringCellValue());
            questionBank.setQbValueD(row.getCell(5).getStringCellValue());
            questionBank.setQbResult(row.getCell(6).getStringCellValue());
            value = row.getCell(7).getStringCellValue();
            if(StrUtil.isNotEmpty(value)){
                questionBank.setQbAnalysis(row.getCell(7).getStringCellValue());
            }
            questionBankList.add(questionBank);
        }
        delete(new EntityWrapper<>());
        if (questionBankList.size() > 0) {
            insertBatch(questionBankList);
        }
        fileInputStream.close();
    }
}
